define(['jquery', 'bootstrap'], function($) {
    class Header {
        constructor () {
            this.loadHTML().then(() => {
                this.search()
                this.userd()
                this.calcCartTotal()
                this.meats()
            })
        }
        loadHTML () {
            // 在这里把方法封装成一个promise，这样就可以在构造函数里处理加载成功之后的逻辑，来调用其他方法
            return new Promise(resolve => {
                // $('header').load('/html/modules/header.html', () => {
                //     resolve()
                // })
                $('header').load('/html/modules/header.html', resolve)
            })
        }
        search () {
            window.jsonp = function jsonp (url, cbName, query) {
                url += `?cb=${cbName}`
                if (query) {
                    for (var key in query) {
                        url += `&${key}=${query[key]}`
                    }
                }
                // 创建script
                var script = document.createElement('script')
                script.src = url
                document.body.appendChild(script) // 因为是一个异步请求，所以只要script存在请求就发出去了，那就一定会返回
                document.body.removeChild(script) // 过河拆桥
            }          
            window.fn =function fn(resp) {
                console.log(resp)
                const {s} =resp
                const ul =document.querySelector('.nav_c ul')
                ul.innerHTML=s.reduce((html,item)=>{
                    html +=`<li>${item}</li>`
                    return html
                },'')
                const meats = document.querySelectorAll('.nav_c li')
                const input = document.querySelector('#input_search')
                for (let i = 0; i < meats.length; i++) {
                    meats[i].onmousedown = function () {
                        input.value = this.innerHTML
                        ul.style.display = 'none'
                    }
                }
            }           
            // 搜索功能            
            $('#input_search').on('keyup', function () {
                jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su','fn',{wd:$(this).val()})
            })
        }
        

        userd(){
            window.setCookie =function setCookie (key, value, option) {
                var cookie = `${key}=${encodeURIComponent(value)}`
                if (option) {
                    if (option.expires) {
                        var d = new Date()
                        d.setDate(d.getDate() + option.expires)
                        cookie += `;expires=${d.toUTCString()}`
                    }
                    if (option.path) {
                        cookie += `;path=${option.path}`
                    } else {
                        // 传了option但是不传path默认根路径
                        cookie += ';path=/'
                    }
                } else {
                    // option没传，也让path默认根路径
                    cookie += ';path=/'
                }
                document.cookie = cookie
            }
            window.getCookie = function  getCookie (key) {
                var str = document.cookie
                var arr = str.split('; ')
                var cookies = arr.reduce((obj, item) => {
                    var subArr = item.split('=')
                    obj[subArr[0]] = decodeURIComponent(subArr[1])
                    return obj
                }, {})
                return cookies[key]
            }
            var unLogin = document.querySelector('#unLogin')
            var isLogin = document.querySelector('#isLogin')
            var nameWrap = isLogin.querySelector('b')
            var username = getCookie('loginUser')
            if (username) {
                unLogin.style.display = 'none'
                isLogin.style.display = 'block'
                nameWrap.innerHTML = username
            }

            document.querySelector('#logout').onclick = function () {
                if (confirm('确定要退出吗？')) {
                    // 删除cookie
                    setCookie('loginUser', '', { expires: -1 })
                    unLogin.style.display = 'block'
                    isLogin.style.display = 'none'
                }  
            }
        }
        calcCartTotal () {
            // 计算购物车总数量
            let count = 0
            let cart = localStorage.getItem('cart')
            if (cart) {
                cart = JSON.parse(cart)
                count = cart.reduce((total, shop) => total + shop.count, 0)
            }
            $('#shopnum').html(count)
            $('#gwnum span').html(count)
        }
        
        meats(){
            this.$span =$('.meat span')
            this.$ul =$('.meat ul')
            this.$meats=$('.meat-list li')

            this.$span.on('mouseenter',e=>{
               this.$ul.css('display','block')
            })
            this.$span.on('mouseleave',e=>{
                this.$ul.css('display','none')
             })

        }
    }
    return new Header()
})